package com.xinyun.server;

import com.xinyun.server.thread.CheckBoardIdThread;
import com.xinyun.server.thread.ScanAllThread;
import com.xinyun.server.thread.SyncTimeThread;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Connection;


/**
 * Created by bubusy on 16/5/21.
 */
public class Server {

    private int port;

    private Log log = LogFactory.getLog(Server.class);

    public Server(int port) {
        this.port = port;
    }

    public void run() throws Exception {

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {

            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new ServerInitializer())
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture f = b.bind(port).sync();


            log.info("服务器已启动,监听端口:" + this.port);

            new ScanAllThread(f).start();
            new CheckBoardIdThread(f).start();
            new SyncTimeThread(f).start();

            f.channel().closeFuture().sync();

        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {

        int port = 8080;
        String dbHost = "localhost";
        int dbPort = 3306;
        String dbName = "fv";
        String dbUser = "root";
        String dbPassword = "";
        Configurations configs = new Configurations();
        try {
            Configuration config = configs.properties("config.properties");
            port = config.getInt("server.port");
            dbHost = config.getString("db.host");
            dbPort = config.getInt("db.port");
            dbName = config.getString("db.name");
            dbUser = config.getString("db.user");
            dbPassword = config.getString("db.password");

        } catch (ConfigurationException ce) {

        }

        String url = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName;
        String user = dbUser;
        String password = dbPassword;

        BasicDataSource basicDs = new BasicDataSource();
        basicDs.setUrl(url);
        basicDs.setUsername(user);
        basicDs.setPassword(password);
        DB.dataSource = basicDs;

        Connection conn = basicDs.getConnection();
        conn.close();
        LogFactory.getLog(Server.class).info("链接数据库成功");

        new Server(port).run();


    }
}
